﻿Imports Newtonsoft.Json

Namespace Catalog
    Public Class BPOSearchResponse
        Public metadata As BPOSearchMetadata
        Public results As ICollection(Of BPOBase)
    End Class

    Public Class BPOSearchMetadata
        Public host As String
        Public path As String
        Public query As String
        Public page As Integer
        Public perPage As Integer
        Public count As Integer
        Public totalPages As Integer
        Public nextQuery As String
        Public perPageOptions As ICollection(Of BPOPerPageOption)
        Public facets As BPOSearchMetadataFacet
        Public appliedFacets As ICollection(Of BPOSearchMetadataFacet)
        Public spellcheck As BPOSearchMetadataSpellCheck

        Public Class BPOPerPageOption
            Public value As Integer
            Public applyQuery As String
        End Class

        Public Class BPOSearchMetadataFacet
            Public fields As ICollection(Of String)
        End Class

        Public Class BPOSearchMetadataSpellCheck
            Public suggestions As ICollection(Of String)
        End Class
    End Class

    Public Class BPOCountObject
        Public count As Integer
    End Class

    Public Class BeatportObjectJsonCoverter
        Inherits JsonConverter

        Public Overrides Function CanConvert(objectType As System.Type) As Boolean
            Return (objectType Is GetType(BPOBase) OrElse objectType.BaseType Is GetType(BPOBase))
        End Function

        Public Overrides Function ReadJson(reader As Newtonsoft.Json.JsonReader, objectType As System.Type, existingValue As Object, serializer As Newtonsoft.Json.JsonSerializer) As Object
            Select Case reader.TokenType
                Case JsonToken.StartArray
                    Dim srJson As Linq.JArray = serializer.Deserialize(reader)
                    If srJson.Count = 0 Then
                        Return Nothing
                    End If
                    'Return New List(Of BPOBase)
                Case Else
                    Dim srJson = serializer.Deserialize(reader)
                    Dim srno As BPOBase

                    Try
                        If srJson IsNot Nothing AndAlso
                            srJson.GetType() Is GetType(Linq.JObject) Then
                            srno = CType(srJson, Linq.JObject).ToObject(Of BPOBase)()
                        Else
                            Return Nothing
                        End If

                        Select Case srno.type
                            Case "release"
                                Return srJson.ToObject(Of BPORelease)()
                            Case "label"
                                Return srJson.ToObject(Of BPOLabel)()
                            Case "artist"
                                Return srJson.ToObject(Of BPOArtist)()
                            Case "chart"
                                Return srJson.ToObject(Of BPOChart)()
                            Case "track"
                                Return srJson.ToObject(Of BPOTrack)()
                            Case "remixer"
                                Return srJson.ToObject(Of BPORemixer)()
                            Case "djprofile"
                                Return srJson.ToObject(Of BPODjProfile)()
                            Case "genre"
                                Return srJson.ToObject(Of BPOGenre)()
                            Case "mix"
                                Return srJson.ToObject(Of BPOMix)()
                            Case "pack"
                                Return srJson.ToObject(Of BPOPack)()
                            Case Else
                                Select Case objectType
                                    Case GetType(BPORelease)
                                        Return srJson.ToObject(Of BPOGenre)()
                                    Case GetType(BPOLabel)
                                        Return srJson.ToObject(Of BPOLabel)()
                                    Case GetType(BPOArtist)
                                        Return srJson.ToObject(Of BPOArtist)()
                                    Case GetType(BPOChart)
                                        Return srJson.ToObject(Of BPOChart)()
                                    Case GetType(BPOTrack)
                                        Return srJson.ToObject(Of BPOTrack)()
                                    Case GetType(BPORemixer)
                                        Return srJson.ToObject(Of BPORemixer)()
                                    Case GetType(BPODjProfile)
                                        Return srJson.ToObject(Of BPODjProfile)()
                                    Case GetType(BPOGenre)
                                        Return srJson.ToObject(Of BPOGenre)()
                                    Case Else
                                        Return Nothing
                                End Select
                        End Select

                    Catch ex As Exception
                        Dim ff = 0
                    End Try

                    Return srJson
            End Select

            Return Nothing
        End Function

        Public Overrides Sub WriteJson(writer As Newtonsoft.Json.JsonWriter, value As Object, serializer As Newtonsoft.Json.JsonSerializer)
            Throw New NotImplementedException()
        End Sub
    End Class

    Public Class BeatportObjectListJsonCoverter
        Inherits JsonConverter

        Public Overrides Function CanConvert(objectType As System.Type) As Boolean
            Return (objectType Is GetType(ICollection(Of BPOBase)))
        End Function

        Public Overrides Function ReadJson(reader As Newtonsoft.Json.JsonReader, objectType As System.Type, existingValue As Object, serializer As Newtonsoft.Json.JsonSerializer) As Object
            Select Case reader.TokenType
                Case JsonToken.StartArray, JsonToken.EndArray
                    Return serializer.Deserialize(Of IList(Of BPOBase))(reader)
                Case JsonToken.Null
                    Return Nothing
                Case Else
                    Dim srJson As BPOBase = serializer.Deserialize(Of BPOBase)(reader)
                    Return New List(Of BPOBase)({srJson})
            End Select
        End Function

        Public Overrides Sub WriteJson(writer As Newtonsoft.Json.JsonWriter, value As Object, serializer As Newtonsoft.Json.JsonSerializer)
            Throw New NotImplementedException()
        End Sub
    End Class
End Namespace